﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Drawing.Drawing2D;
using BusinessLogic;
using BusinessObject;
using Utility;
using System.Web.Security;
using System.Resources;
namespace WebTeamplate
{
    public partial class Admin_GenerateBarcodeForMH : BasePage
    {
        public MatHang objMatHang = new MatHang();
        public string xe = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                string id = CommonFunction.GetValueFromQueryString(this.Page, "id");
                if (id != string.Empty)
                {
                    objMatHang = MatHangUtil.Get(id);
                    if (objMatHang != null)
                    {
                        DMXe objXe = DMXeUtil.Get(objMatHang.MaXe);
                        if (objXe != null)
                            xe = objXe.TenXe;
                    }
                }
            }
        }
        protected void btnExportBarCode_Click(object sender, EventArgs e)
        {
            string id = CommonFunction.GetValueFromQueryString(this.Page, "id");
            Session.Add("idBarCode", id + ";");
            Session["slBarCode"] = txtSoLuongMaVach.Value + ";";
            if (id != string.Empty)
            {
                //delete all barcode
                string s = Server.MapPath("~/Admin/BarCode/Images/");
                string[] filePaths = Directory.GetFiles(s);
                foreach (string filePath in filePaths)
                {
                    try
                    {
                        File.Delete(filePath);
                    }
                    catch
                    {
                    }
                }
                ExportBarCode128(id, string.Empty);
            }
            ScriptManager.RegisterStartupScript(this, this.GetType(), "OpenPopUpBarCode", "OpenPopUpBarCode();", true);
        }
        public void ExportBarCode128(string requestedCode, string price)
        {
            System.Drawing.Image img = GenCode128.Code128Rendering.MakeBarcodeImage(requestedCode, 1, 30, true, true, "arial", 11);
            string s = Server.MapPath("~/Admin/BarCode/Images/");
            System.Drawing.Image img1 = cropImage(img, new Rectangle(0, 0, 121, 46));
            img1.Save(s + requestedCode + ".gif");
        }
        private System.Drawing.Image cropImage(System.Drawing.Image img, Rectangle cropArea)
        {
            Bitmap bmpImage = new Bitmap(img);
            Bitmap bmpCrop = bmpImage.Clone(cropArea,
            bmpImage.PixelFormat);
            return (System.Drawing.Image)(bmpCrop);
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("DMMatHang.aspx");
        }
    }
}
